﻿#include "AssetEvent.h"

#include <time.h>
#include <iostream>
#include "CommonTool.h"



string AssetManager::DeviceRegistration::BuildJson()
{
	

	rapidjson::Document doc;
	doc.SetObject();
	rapidjson::Document::AllocatorType& allocator = doc.GetAllocator();
	//header
	//AssetManager::BuildHeaderJson(deviceRegistration,doc, allocator);
	rapidjson::Value valueHeader(rapidjson::kStringType);
	rapidjson::Value objectHeader(rapidjson::kObjectType);

	
	string str = "deviceRegistration";
	valueHeader.SetString(str.c_str(), str.length());
	objectHeader.AddMember("FunName", valueHeader, allocator);

	string uuid = CommonTool::GetUUID();
	valueHeader.SetString(uuid.c_str(), uuid.size());
	objectHeader.AddMember("Sid", valueHeader, allocator);

	time_t  ltime = time(NULL);
	char  buff[128] = { 0 };
	sprintf(buff, "%lld", ltime);
	valueHeader.SetString(buff, strlen(buff));
	objectHeader.AddMember("Timestamp", valueHeader, allocator);

	doc.AddMember("Header", objectHeader, allocator);
	//data
	rapidjson::Value ObjectKey(rapidjson::kObjectType);
	rapidjson::Value valueData(rapidjson::kStringType);

	valueData.SetString(GUID.c_str(), GUID.size());
	ObjectKey.AddMember(rapidjson::StringRef(AssetField::GUID.c_str()), valueData, allocator);
	valueData.SetString(partition.c_str(), partition.size());
	ObjectKey.AddMember(rapidjson::StringRef(AssetField::partition.c_str()), valueData, allocator);
	valueData.SetString(IP.c_str(), IP.size());
	ObjectKey.AddMember(rapidjson::StringRef(AssetField::IP.c_str()), valueData, allocator);
	valueData.SetString(deviceName.c_str(), deviceName.size());
	ObjectKey.AddMember(rapidjson::StringRef(AssetField::deviceName.c_str()), valueData, allocator);
	valueData.SetString(innerName.c_str(), innerName.size());
	ObjectKey.AddMember(rapidjson::StringRef(AssetField::innerName.c_str()), valueData, allocator);
	valueData.SetString(voltageCalss.c_str(), voltageCalss.size());
	ObjectKey.AddMember(rapidjson::StringRef(AssetField::voltageCalss.c_str()), valueData, allocator);
	valueData.SetString(deviceCode.c_str(), deviceCode.size());
	ObjectKey.AddMember(rapidjson::StringRef(AssetField::deviceCode.c_str()), valueData, allocator);
	valueData.SetString(corpID.c_str(), corpID.size());
	ObjectKey.AddMember(rapidjson::StringRef(AssetField::deviceCode.c_str()), valueData, allocator);
	valueData.SetString(siteType.c_str(), siteType.size());
	ObjectKey.AddMember(rapidjson::StringRef(AssetField::siteType.c_str()), valueData, allocator);
	valueData.SetString(position.c_str(), position.size());
	ObjectKey.AddMember(rapidjson::StringRef(AssetField::siteType.c_str()), valueData, allocator);
	valueData.SetString(factoryID.c_str(), factoryID.size());
	ObjectKey.AddMember(rapidjson::StringRef(AssetField::factoryID.c_str()), valueData, allocator);
	valueData.SetString(model.c_str(), model.size());
	ObjectKey.AddMember(rapidjson::StringRef(AssetField::model.c_str()), valueData, allocator);
	valueData.SetString(agentID.c_str(), agentID.size());
	ObjectKey.AddMember(rapidjson::StringRef(AssetField::agentID.c_str()), valueData, allocator);
	valueData.SetString(softVersion.c_str(), softVersion.size());
	ObjectKey.AddMember(rapidjson::StringRef(AssetField::softVersion.c_str()), valueData, allocator);
	valueData.SetString(busnessSystemID.c_str(), busnessSystemID.size());
	ObjectKey.AddMember(rapidjson::StringRef(AssetField::busnessSystemID.c_str()), valueData, allocator);
	valueData.SetString(user.c_str(), user.size());
	ObjectKey.AddMember(rapidjson::StringRef(AssetField::user.c_str()), valueData, allocator);
	valueData.SetString(principle.c_str(), principle.size());
	ObjectKey.AddMember(rapidjson::StringRef(AssetField::principle.c_str()), valueData, allocator);
	valueData.SetString(telephone.c_str(), telephone.size());
	ObjectKey.AddMember(rapidjson::StringRef(AssetField::telephone.c_str()), valueData, allocator);
	valueData.SetString(addTime.c_str(), addTime.size());
	ObjectKey.AddMember(rapidjson::StringRef(AssetField::addTime.c_str()), valueData, allocator);
	valueData.SetString(updateTime.c_str(), updateTime.size());
	ObjectKey.AddMember(rapidjson::StringRef(AssetField::updateTime.c_str()), valueData, allocator);
	valueData.SetString(remark.c_str(), remark.size());
	ObjectKey.AddMember(rapidjson::StringRef(AssetField::remark.c_str()), valueData, allocator);
	valueData.SetString(systemVersion.c_str(), systemVersion.size());
	ObjectKey.AddMember(rapidjson::StringRef(AssetField::systemVersion.c_str()), valueData, allocator);
	valueData.SetString(normaname.c_str(), normaname.size());
	ObjectKey.AddMember(rapidjson::StringRef(AssetField::normaname.c_str()), valueData, allocator);
	valueData.SetString(scriptName.c_str(), scriptName.size());
	ObjectKey.AddMember(rapidjson::StringRef(AssetField::scriptName.c_str()), valueData, allocator);

	//subtable
	rapidjson::Value ObjectArray1(rapidjson::kArrayType);
	vector<SubTable>::iterator sub = vSubTable.begin();
	for (; sub != vSubTable.end(); sub++)
	{

		rapidjson::Value value(rapidjson::kStringType);
		rapidjson::Value object(rapidjson::kObjectType); 
		//value.SetString("123456", 6);
		value.SetString(sub->subGUID.c_str(), sub->subGUID.size());
		object.AddMember(rapidjson::StringRef(AssetField::subGUID.c_str()), value, allocator);

		value.SetInt(sub->port);
		object.AddMember(rapidjson::StringRef(AssetField::port.c_str()), value, allocator);

		value.SetString(sub->IP.c_str(), sub->IP.size());
		object.AddMember(rapidjson::StringRef(AssetField::IP.c_str()), value, allocator);
 
		value.SetString(sub->MAC.c_str(), sub->MAC.size());
		object.AddMember(rapidjson::StringRef(AssetField::MAC.c_str()), value, allocator);

		value.SetString(sub->corpID.c_str(), sub->corpID.size());
		object.AddMember(rapidjson::StringRef(AssetField::corpID.c_str()), value, allocator);

		value.SetString(sub->updateTime.c_str(), sub->updateTime.size());
		object.AddMember(rapidjson::StringRef(AssetField::updateTime.c_str()), value, allocator);

		value.SetString(sub->networkGUID.c_str(), sub->networkGUID.size());
		object.AddMember(rapidjson::StringRef(AssetField::networkGUID.c_str()), value, allocator);

		value.SetInt(sub->isSpanDcd);
		object.AddMember(rapidjson::StringRef(AssetField::isSpanDcd.c_str()), value, allocator);

		value.SetInt(sub->isNat);
		object.AddMember(rapidjson::StringRef(AssetField::isNat.c_str()), value, allocator);

		value.SetString(sub->subordinateDcdName.c_str(), sub->subordinateDcdName.size());
		object.AddMember(rapidjson::StringRef(AssetField::subordinateDcdName.c_str()), value, allocator);

		value.SetString(sub->subordinateDcdIp.c_str(), sub->subordinateDcdIp.size());
		object.AddMember(rapidjson::StringRef(AssetField::subordinateDcdIp.c_str()), value, allocator);

		value.SetString(sub->subordinateDcdGUID.c_str(), sub->subordinateDcdGUID.size());
		object.AddMember(rapidjson::StringRef(AssetField::subordinateDcdGUID.c_str()), value, allocator);

		ObjectArray1.PushBack(object, allocator);
	}
	ObjectKey.AddMember(rapidjson::StringRef(AssetField::SubTable.c_str()), ObjectArray1, allocator);

	doc.AddMember("Data", ObjectKey, allocator);
	rapidjson::StringBuffer s;
	rapidjson::Writer<rapidjson::StringBuffer> writer(s);
	doc.Accept(writer);

	 
	return string(s.GetString());
}

void AssetManager::BuildHeaderJson(AssetEventType type, rapidjson::Document & doc, rapidjson::Document::AllocatorType&allocator)
{
 
	string strType = g_strEventType[type];
	 
	rapidjson::Value valueHeader(rapidjson::kStringType);
	rapidjson::Value objectHeader(rapidjson::kObjectType);

	strType = "123";
	valueHeader.SetString("123", strType.size());
	objectHeader.AddMember("FunName", valueHeader, allocator);

	string uuid = CommonTool::GetUUID();
	valueHeader.SetString(uuid.c_str(), uuid.size());
	objectHeader.AddMember("Sid", valueHeader, allocator);

	time_t  ltime = time(NULL);
	char  buff[128] = { 0 };
	sprintf(buff, "%lld", ltime);
	valueHeader.SetString(buff, strlen(buff));
	objectHeader.AddMember("Timestamp", valueHeader, allocator);

	doc.AddMember("Header", objectHeader, allocator);



	return ;
}

char * Build::buildLenPack(std::string strJson)
{

		static char buff[16];
		string strBegn = "<?begn?>";
		memset(buff, 0, sizeof(buff));
		memcpy(buff, strBegn.c_str(), strBegn.length());

		buff[8] = 0x00;
		buff[9] = 0x01;

		buff[10] = 0x01;
		buff[11] = 0x01;

		int nlen = strJson.length() + 64;
		int netLen = htonl(nlen);

		memcpy(&buff[12], &netLen, 4);
		return buff;


}

char * Build::buildDeviceConfirmPack(std::string guid, int & resultlen)
{
	rapidjson::Document doc;
	doc.SetObject();
	rapidjson::Document::AllocatorType& allocator = doc.GetAllocator();
	//header
	//AssetManager::BuildHeaderJson(deviceRegistration,doc, allocator);
	rapidjson::Value valueHeader(rapidjson::kStringType);
	rapidjson::Value objectHeader(rapidjson::kObjectType);

	string strtype = "deviceConfirm";
	valueHeader.SetString(strtype.c_str(), strtype.size());
	objectHeader.AddMember("FunName", valueHeader, allocator);

	string sid = CommonTool::GetUUID();
	valueHeader.SetString(sid.c_str(), sid.size());
	objectHeader.AddMember("Sid", valueHeader, allocator);

	char strTime[128] = { 0 };
	long ltime = time(0);
	sprintf(strTime, "%lld", ltime);
	valueHeader.SetString(strTime, strlen(strTime));
	objectHeader.AddMember("Timestamp", valueHeader, allocator);

	doc.AddMember("Header", objectHeader, allocator);

	rapidjson::Value valueData(rapidjson::kStringType);
	rapidjson::Value objectData(rapidjson::kObjectType);

	string type = "0";
	valueData.SetString(type.c_str(), type.size());
	objectData.AddMember("OperationType", valueData, allocator);

	valueData.SetString(guid.c_str(), guid.size());
	objectData.AddMember("DevGUID", valueData, allocator);

	doc.AddMember("Data", objectData, allocator);

	rapidjson::StringBuffer s;
	rapidjson::Writer<rapidjson::StringBuffer> writer(s);
	doc.Accept(writer);

	string json = s.GetString();

	char * cBuff = buildLenPack(json);

	string end = "<?endn?>";
	char * sendBuff = new char[json.length() + 64 + 16 + 8 + 1];
	memset(sendBuff, 0, json.length() + 64 + 16 + 8 + 1);
	memcpy(sendBuff, cBuff, 16);
	memcpy(&sendBuff[16], json.c_str(), json.length());
	memcpy(&sendBuff[json.length()+ 16 + 64], end.c_str(), end.length());

	resultlen = 64 + 16 + 8 + json.length();
	return sendBuff;
}
